home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ddj0190.arc / DOUGLAS.LST next >
File List  |  1989-12-19  |  3KB  |  152 lines

  1. _ERROR MESSAGE MANAGEMENT_
  2. by Rohan Douglas
  3.  
  4. [LISTIN╟ ONE]
  5.  
  6.  
  7.   /* Include file for macros replacement of erro≥ function« */
  8.  
  9.   #define error(m)  _error(__FILE__, __LINE__)
  10.  
  11.   extern  void      _error(char *, int);
  12.  
  13.  
  14. [LISTIN╟ TWO]
  15.  
  16. /* Definition of error function«  */
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "error.h"
  22.  
  23. static int get_err_msg(char *, char *, char *);
  24.  
  25. void
  26. _error(char *fname, int lno)
  27. {
  28.     char *period;  /* Pointer for file extension */
  29.     char lstr[6];  /* Temp string for file line */
  30.     char key[15];  /* Message key (file:line) */
  31.     char msg[75];  /* Error message from file */
  32.  
  33.     /* Strip out file extension from file name */
  34.     if ((period = strchr(fname, '.')) != NULL)
  35.         *period = '\0';
  36.  
  37.     /* Create file:linenumber key */
  38.     sprintf(key, "%s:%s", fname, itoa(lno, lstr,
  39.        10));
  40.  
  41.     /* Look up error message from file */
  42.     if (!get_err_msg(fname, key, msg))
  43.         /* Not found in file, just print out
  44.          * file:lineno.
  45.          */
  46.         printf("Error [%s]", key);
  47.     else 
  48.         printf("%s [%s]", msg, key);
  49.     return;
  50. } /* error() */
  51.  
  52. static int
  53. get_err_msg(char *fname, char *key, char *msg)
  54. {
  55.     FILE *fp;      /* Error message file pointer */
  56.     char msg_key[14];/* Key for current line */
  57.  
  58.     /* Open error file */
  59.     if (!(fp = fopen("error.dat", "r")))
  60.         return FALSE;
  61.  
  62.     /* Scan file for message */
  63.     while (!feof(fp)) {
  64.         fscanf(fp, " %s ", msg_key);
  65.         fgets(msg, 75, fp);
  66.         if (!strcmpi(key, msg_key))
  67.             break;
  68.     }è    fclose(fp);
  69.  
  70.     /* Return false if message not found */
  71.     if (feof(fp))
  72.         return FALSE;
  73.  
  74.     /* Remove CR from message string */
  75.     msg[strlen(msg)-1] = '\0';
  76.     return TRUE;
  77. } /* get_err_msg() */
  78.  
  79.  
  80. [LISTIN╟ THREE]
  81.  
  82.  
  83. BEGIN {
  84.     FS = "\"";
  85.     printf("") > "error.txt";
  86.     printf("") > "error.dat";
  87. }
  88. / *error *\(/ {
  89.     printf("%s :\n", $2) >> "error.txt";
  90.     ind = substr(FILENAME, 0, index(FILENAME, ".")
  91.       - 1);
  92.     printf("%s:%d\t%s\n", ind, NR, $2) >>
  93.       "error.dat";
  94.     getline();
  95.     i = index($0, "/* ");
  96.     if (i) i++;
  97.     while (i) {
  98.         printf("\t%s\n", substr($0, i + 2)) >>
  99.           "error.txt";
  100.         getline();
  101.         if (index($0, "*/")) break;
  102.         i = index($0, "* ");
  103.     }
  104. }
  105.  
  106.  
  107. Figure 1: Sample source file with embedded error message and
  108. description. 
  109.  
  110.  
  111. #include <stdio.h> 
  112. #include "error.h" 
  113.  
  114. main(int argc, char *argv[]) 
  115.     if (argc == 1) 
  116.         error("Missing parameters"); 
  117.         /*  No parameters have been passed 
  118.          * to this test program. This error message 
  119.          *  will be displayed as an example if no 
  120.          *  parameters are passed to this test 
  121.          *  program. 
  122.          */
  123.     exit(0);
  124. }
  125.  
  126.  
  127.  
  128. Figure 2: The user manual list resulting from Figure 1.
  129.  
  130. Missing parameters : 
  131.      No parameters have been passed to 
  132.      this test program. This error message 
  133.      will be displayed as an example if no 
  134.      parameters are passed to this test 
  135.      program. 
  136.  
  137.  
  138.  
  139. Figure 3: The message database resulting from Figure 1.
  140.  
  141.  
  142. test:8  Missing parameters
  143.  
  144.  
  145. Figure 4: The results of the sample program.
  146.  
  147.  
  148. Missing parameters [test:8]
  149.  
  150.  
  151.